from typing import Any

from src.baseapp.controller.baseController import BaseController
from src.baseapp.model.BaseOrmOpeator import OrmOperator
from src.baseapp.model.testcaseServiceModel.testcaseModel import TestCaseModel
from src.baseapp.controller.caseService.directoryController import DirectoryController
from src.error.exceptions import CaseNotFoundException


class CaseController(BaseController):
    def __init__(self):
        super().__init__()
        self.orm = OrmOperator(TestCaseModel)
        self.directory_controller = DirectoryController()

    def add_case(self, cases_info: list[dict]) -> tuple:
        """
        添加用例
        :param cases_info: 用例信息 - array
        :return:
        """
        response = self.response_data()
        try:
            models = [TestCaseModel(**case_info) for case_info in cases_info]
            self.orm.insert_data(models)
        except Exception as e:
            response.code = 500
            response.msg = str(e)
        return response.code, response.to_dict
    
    def delete_case(self, case_ids: list[int]) -> tuple:
        """
        删除用例
        :param case_ids: 用例id - array
        :return:
        """
        response = self.response_data()
        try:
            cases: list[TestCaseModel] = self.orm.in_here(TestCaseModel.case_id, case_ids).equal(TestCaseModel.is_delete, 0).get_all()
            for case in cases:
                case.is_delete = 1
            self.orm.update_data(cases)
        except Exception as e:
            response.code = 500
            response.msg = str(e)
        return response.code, response.to_dict
    
    def update_case(self, case_ids: list[int], modify_info: dict[str, Any]) -> tuple:
        """
        更新用例
        :param case_ids: 用例id - array
        :param modify_info: 修改信息 - dict
        :return:
        """
        response = self.response_data()
        try:
            cases: list[TestCaseModel] = self.orm.in_here(TestCaseModel.case_id, case_ids).equal(TestCaseModel.is_delete, 0).get_all()
            for case in cases:
                for key, value in modify_info.items():
                    setattr(case, key, value)
            self.orm.update_data(cases)
        except Exception as e:
            response.code = 500
            response.msg = str(e)
        return response.code, response.to_dict
    
    def get_case_list(self, conditions: dict[str, Any]) -> tuple:
        field_condition_mapping = {
            "directory_id": TestCaseModel.directory_id,
            "testcase_id": TestCaseModel.testcase_id,
            "name": TestCaseModel.name,
            "priority": TestCaseModel.priority,
            "creator": TestCaseModel.user_id,
            "version_id": TestCaseModel.version_id,
            "create_time": TestCaseModel.create_time,
            "update_time": TestCaseModel.update_time,
            "is_delete": TestCaseModel.is_delete,
        }
        response = self.response_data()
        try:
            for key, value in conditions.items():
                if key == 'directory_id':
                    # todo 需要查询此目录及其子目录下所有用例
                    directory_ids = self.directory_controller.get_all_directory_id_by_id(value)
                    self.orm = self.orm.in_here(field_condition_mapping[key], directory_ids)
                elif key == 'name':
                    self.orm = self.orm.like(field_condition_mapping[key], value)
                elif key == 'priority':
                    self.orm = self.orm.in_here(field_condition_mapping[key], value)
                elif key in ('create_time', 'update_time'):
                    self.orm = self.orm.between(field_condition_mapping[key], value[0], value[1])
                else:
                    self.orm = self.orm.equal(field_condition_mapping[key], value)
            else:
                cases: list[TestCaseModel] = self.orm.get_all()
                response.data['cases'] = [
                    {
                        "testcase_id": case.testcase_id,
                        "name": case.name,
                        "priority": case.priority,
                        "creator": case.user_id,
                        "version_id": case.version_id,
                        "create_time": case.create_time,
                        "update_time": case.update_time,
                    }
                    for case in cases
                ]
        except Exception as e:
            response.code = 500
            response.msg = str(e)
        return response.code, response.to_dict
    
    def get_case_detail(self, case_id: int):
        response = self.response_data()
        try:
            case: TestCaseModel = self.orm.equal(TestCaseModel.testcase_id, case_id).equal(TestCaseModel.is_delete, 0).get_one()
            response.data['case'] = {
                "testcase_id": case.testcase_id,
                "directory_id": case.directory_id,
                "name": case.name,
                "priority": case.priority,
                "description": case.description,
                "condition": case.condition,
                "steps": case.steps,
                "expected_result": case.expected_result,
                "version_id": case.version_id,
                "creator": case.user_id,
                "create_time": case.create_time,
                "update_time": case.update_time,
            }
        except Exception as e:
            response.code = 500
            response.msg = str(e)
        return response.code, response.to_dict

    
    
